package com.hp.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.hp.ssm.entity.Cases;
import com.hp.ssm.entity.User;
import com.hp.ssm.service.CaretakerService;
import com.hp.ssm.util.GeneratID;
import com.hp.ssm.util.ResultJson;
import com.hp.ssm.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("CaretakerController")
public class CaretakerController {

    @Autowired
    private CaretakerService caretakerService;

    @RequestMapping("selectAll")
    @ResponseBody
    public ModelAndView selectAll(HttpServletResponse resp,User user,Integer page,Integer limit){
        System.out.println(user);
        ModelAndView mv=new ModelAndView();
        List<User> list=caretakerService.selectAll(user,page,limit);
        PageInfo<User> userPageInfo = new PageInfo<User>(list);
        ResultJson success=ResultJson.success(userPageInfo);
        Utils.returnJson(success,resp);
        return mv;
    }

    @RequestMapping("add")
    public void add(User user,HttpServletResponse response){
        Date  atime = new Date();
        user.setAtime(atime);
        String apuser = GeneratID.getGeneratID();
        user.setApuser(apuser);
        int i = caretakerService.add(user);
        if (i>0){
            ResultJson success=ResultJson.success(i);
            Utils.returnJson(success,response);
        }else {
            ResultJson error = ResultJson.error();
            Utils.returnJson(error,response);
        }
    }
    @RequestMapping("delete")
    public void delete(int aid,HttpServletResponse response){
        int i = caretakerService.delete(aid);
        ResultJson success=ResultJson.success(i);
        Utils.returnJson(success,response);
    }

    @RequestMapping("findAll")
    @ResponseBody
    public void findAll(int aid,HttpServletRequest req, HttpServletResponse resp){
        User user= caretakerService.findAll(aid);
        System.out.println(user);
        ResultJson success=ResultJson.success(user);
        Utils.returnJson(success,resp);
    }
    @RequestMapping("update")
    public void update(User user,HttpServletResponse response){
        int i = caretakerService.update(user);
        if (i>0){
            ResultJson success=ResultJson.success(i);
            Utils.returnJson(success,response);
        }else {
            ResultJson error = ResultJson.error();
            Utils.returnJson(error,response);
        }
    }
    @RequestMapping("deleteAll")
    public String deleteAll(User user,Integer page,Integer limit, HttpServletResponse resp){
        List<User> list=caretakerService.deleteAll(user,page,limit);
        PageInfo<User> userPageInfo = new PageInfo<User>(list);
        ResultJson success=ResultJson.success(userPageInfo);
        Utils.returnJson(success,resp);
        return null;
    }

    @RequestMapping("recover")
    public void recover(int aid,HttpServletResponse response){
        int i = caretakerService.recover(aid);
        ResultJson success=ResultJson.success(i);
        Utils.returnJson(success,response);
    }
}
